auto.waitFor();

var width = device.width;
var height = device.height
setScreenMetrics(width, height);
var myFunc = require("./myCommonFunction.js");

var myWord = require("./myWord.js");


// var ret = myFunc.initNetWork(null, true);

var wordRes = null;
var haoyou = null;
var moreTask = null;
// zixuan()
// zixuan2()
opreateApp();

// log(myWord.findWordLocation("签到"));
// myWord.isLog = true;
// wordRes = myWord.getAllword();
// zixuan()

function openWeixin() {
    myFunc.openMyApp("微信");
    sleep(800);
    // if (textContains("通讯录").findOne(1000) == null) {
    // myFunc.log("打开两个微信");
    // textContains("以下方式").findOne(5000)
    // var a = textContains("微信").find()[0];
    // myFunc.clickCenter(a);
    // sleep(1000);
    if (textContains("通讯录").findOne(1000) == null) {
        var mima = textContains("密码").findOne(1000);
        if (mima != null) {
            mima.click();
            // textContains("密码").findOnce().click();
            sleep(1000);
            //解密需要, 不能删
            let key = myFunc.key
            for (a of myFunc.jiemi('微信隐私密码')) {
                desc(a).findOnce().click();
                sleep(400);
            }
            // textContains("忘记密码").findOne(1000);
            // click(260, 2700);
            // sleep(1000);
            // click(740, 2460);
            // sleep(1000);
            // click(260, 2200);
            // sleep(1000);
            // click(1200, 2460);

        }
    }
    // }
}

function opreateApp() {
    try {
        console.log("开始opreateApp");
        openWeixin();
        sleep(1000);
        scrollUp(0);
        // text("微证券").findOne(3000).parent().parent().click();
        sleep(2000);
        log("查微证券");
        if (!myWord.findClickWord("微证券")) {
            myFunc.tuichu("微信");
            opreateApp();
            return;
        }
        sleep(3000);
        log("点微证券");
        // click(374, 3025);
        if (myFunc.clickByText("微证券", 1000)) {

        } else if (!myWord.findOneClick("微证券", 2, 1, false)) {
            click(300, 2332);
        }
        // myFunc.textpClick('微证券')
        sleep(3000);
        log("点我的");
        var wd = myFunc.findOne('我的', 1000);
        if (wd != null) {
            myFunc.clickCenter(wd);
        } else {
            click(950, 2362);
        }

        // myFunc.textpClick('我的', 5000)
        sleep(3000);
        // textContains("福利中心").findOne(5000).click();
        log("点福利中心");
        // 
        var fl = myFunc.findOne('福利中心', 1000);
        if (fl != null) {
            log("click by text");
            myFunc.clickCenter(fl);
        } else if (!myWord.findClickWord("福利中心")) {
            click(180, 1300);
        }
        // myFunc.textpClick('福利中心')
        sleep(5000);
        zixuan();
        zixuan2();
        console.log("end opreateApp");
        myFunc.tuichu("微信");
    } catch (error) {
        log(error);
        myFunc.tuichu("微信");
        opreateApp();
    }

}

function getTask(task, status) {
    var start = false;
    for (var index = 0; index < wordRes.length; index++) {
        var element = wordRes[index];
        var word = element.words;
        if (start) {
            if (word.indexOf("去完成") != -1) {
                status.push(element.location);
            } else {
                task.push(element);
            }
        }
        if (start || word.indexOf("今日任务") != -1) {
            start = true;
        }
    }
}

function doRenwu(taskText, word, callback) {
    // if (taskText.indexOf(callback.name) != -1) {  
    if (taskText.indexOf(word) != -1) {
        log('do', word);
        callback();
        return true;
    }
    return false;
}


function zixuan() {
    sleep(2000);
    if (myFunc.findOnce('今日任务') == null) {
        wordRes = myWord.getAllword();
        if (!myWord.wordExist("今日任务", wordRes)) {
            log("没有找到今日任务");
            myFunc.tuichu("微信");
            opreateApp();
            return;
        }
    }
    var wd = myFunc.findOne('更多任务', 1000, true);
    if (wd != null) {
        wd.click();
        var qwc = myFunc.findOnce('去完成');
        if (qwc == null) {
            return;
        }
        wordRes = [];
        myFunc.bianli(qwc.parent(), false, wordRes);
    } else if (moreTask == null) {
        wordRes = myWord.getAllword();
        moreTask = myWord.findWordLocation('更多任务', wordRes);
        myWord.clickLocation(moreTask);
    }
    sleep(1000);
    for (var index = wordRes.length - 1; index > -1; index--) {
        var element = wordRes[index];
        var word = '';
        if (wd != null) {
            word = element.text();
        } else {
            word = element.words;
        }
        if (word.indexOf("去完成") != -1) {
            var taskText = '';
            if (wd != null) {
                taskText = wordRes[index - 2].text();
            } else {
                taskText = wordRes[index - 1].words;
            }
            if (taskText.indexOf('分享') != -1 || taskText.indexOf('桌面') != -1) {
                continue;
            }
            if (wd != null) {
                element.click();
            } else {
                myWord.clickLocation(element.location);
            }

            log(taskText, index);
            if (doRenwu(taskText, '添加精选', 添加精选)) {
                zixuan();
                return;
            }
            if (doRenwu(taskText, '添加一只热搜', 添加一只热搜)) {
                zixuan();
                return;
            }
            if (doRenwu(taskText, '观看视频', 观看视频)) {
                zixuan();
                return;
            }
            if (doRenwu(taskText, '大盘行情', 大盘行情)) {
                zixuan();
                return;
            }
            if (doRenwu(taskText, '阅读一篇资讯', 阅读一篇资讯)) {
                zixuan();
                return;
            }
            if (doRenwu(taskText, '题材热点', 题材热点)) {
                zixuan();
                return;
            }
            if (doRenwu(taskText, '股票扫雷', 股票扫雷)) {
                zixuan();
                return;
            }
            if (doRenwu(taskText, '浏览社区帖子', 浏览社区帖子)) {
                zixuan();
                return;
            }
        }
    }
}

function zixunByWord() {
    if (myFunc.findOnce('今日任务') == null) {
        log("没有找到今日任务");
        myFunc.tuichu("微信");
        opreateApp();
        return;
    }
    var qwc = myFunc.findOnce('去完成');
    if (qwc == null) {
        return;
    }
    var wordRes = [];
    myFunc.bianli(qwc.parent(), false, wordRes);
    for (var index = wordRes.length - 1; index > -1; index--) {
        var element = wordRes[index];
        var word = element.text();
        if (word.indexOf("去完成") != -1) {
            var taskText = wordRes[index - 2].text();
            element.click();
            log(taskText, index);
            doRenwu(taskText, '股票扫雷', 股票扫雷);
            doRenwu(taskText, '添加精选', 添加精选);
            doRenwu(taskText, '添加一只热搜', 添加一只热搜);
            doRenwu(taskText, '观看视频', 观看视频);
            doRenwu(taskText, '大盘行情', 大盘行情);
            doRenwu(taskText, '阅读一篇资讯', 阅读一篇资讯);
            doRenwu(taskText, '题材热点', 题材热点);
            doRenwu(taskText, '股票扫雷', 股票扫雷);
            doRenwu(taskText, '猜涨跌', 猜涨跌);
        }
    }

}

function zixuan2() {
    var wd = myFunc.findOnce('今日任务');
    if (wd == null) {
        wordRes = myWord.getAllword();
        if (!myWord.wordExist("今日任务", wordRes)) {
            log("没有找到今日任务");
            myFunc.tuichu("微信");
            opreateApp();
            return;
        }
    } else {
        var qwc = myFunc.findOnce('去完成');
        if (qwc == null) {
            return;
        }
        wordRes = [];
        myFunc.bianli(qwc.parent(), false, wordRes);
    }
    sleep(1000);
    for (var index = wordRes.length - 1; index > -1; index--) {
        var element = wordRes[index];
        var word = '';
        if (wd != null) {
            word = element.text();
        } else {
            word = element.words;
        }
        if (word.indexOf("去完成") != -1) {
            var idx = index;
            if (wd != null) {
                taskText = wordRes[index - 1].text();
            } else {
                taskText = wordRes[index - 1].words;
            }
            for (let index = 0; index < 3; index++) {
                if (taskText.indexOf('分享') == -1 || taskText.indexOf('金币') != -1) {
                    if (wd != null) {
                        taskText = wordRes[--idx].text();
                    } else {
                        taskText = wordRes[--idx].words;
                    }
                }

            }
            if (index - idx > 2) {
                continue;
            }

            if (wd != null) {
                myFunc.clickCenter(element);
                // element.click();
            } else {
                myWord.clickLocation(element.location);
            }
            log(taskText, index);

            doRenwu(taskText, '分享微证券', 分享微证券);
            doRenwu(taskText, '分享一篇资讯', 分享一篇资讯);
            doRenwu(taskText, '分享个股', 分享个股);
        }
    }

}


function 分享微证券() {
    sleep(2000);
    openWeixinHaoyou();
    sleep(5000);
    // var a = textContains("大盘行情").findOne(1000);
    // if (a != null && getQuWanCheng(a) != null) {
    //     sleep(1000);
    //     click(width - 100, a.bounds().centerY());
    //     sleep(8000);
    //     clickByText("返回查看");
    //     textContains("今日任务").findOne(5000);
    // }
}

function 大盘行情() {
    sleep(12000);
    back();
    sleep(1000);
    // var a = textContains("大盘行情").findOne(1000);
    // if (a != null && getQuWanCheng(a) != null) {
    //     sleep(1000);
    //     click(width - 100, a.bounds().centerY());
    //     sleep(8000);
    //     clickByText("返回查看");
    //     textContains("今日任务").findOne(5000);
    // }
}



function 分享个股() {
    sleep(3000);
    if (!myFunc.clickByText("1", 1000)) {
        click(80, 1100);
    }
    sleep(3000);
    openWeixinHaoyou();
    sleep(2000);
    back();
    sleep(500);
    back();
    sleep(2000);
    // var a = textContains("分享个股").findOne(1000);
    // if (a != null && getQuWanCheng(a) != null) {
    //     click(width - 100, a.bounds().centerY())
    //     sleep(1000);
    //     clickByText("NO.1");
    //     sleep(1000);
    //     text("分享").findOne(5000);
    //     sleep(1000);
    //     openWeixinHaoyou();
    //     sleep(1000);
    //     clickByText("更多福利");
    //     sleep(1000);
    //     textContains("今日任务").findOne(5000);
    // }
}


function 分享一篇资讯() {
    sleep(3000);
    // click(120, 700);
    var a = myFunc.findOnce("查看更多");
    if (a == null) {
        a = myWord.findWordLocation('查看更多');
        click(a.left, a.top + 200)
    } else {
        click(a.bounds().left, a.bounds().top + 200);
    }
    sleep(2500);
    openWeixinHaoyou();
    sleep(1500);
    back();
    sleep(500);
    back();
    sleep(3000);
    // var a = textContains("分享一篇资讯").findOne(1000);
    // if (a != null && getQuWanCheng(a) != null) {
    //     click(width - 100, a.bounds().centerY());
    //     text("要闻").findOne(1000);
    //     sleep(1000);
    //     click(720,2134);
    //     sleep(1000);
    //     openWeixinHaoyou();
    //     sleep(1000);
    //     clickByText("更多福利");
    //     sleep(1000);
    //     textContains("今日任务").findOne(5000);
    // }
}
function openWeixinHaoyou() {
    sleep(500);
    click(1000, 190);
    sleep(1500);
    if (!myFunc.clickByText("心之所向", 1000) && haoyou == null) {
        haoyou = myWord.findWordLocation("心之所向");
        sleep(800);
        myWord.clickLocation(haoyou);
    } else {
        click(135, 1355);
    }
    text("发送").findOne(5000).click();

    // clickByText("发送");
    // sleep(500);
    // clickByText("心之所向");
    // sleep(500);
    // text("发送").findOne(5000).click();
}


function 自选() {
    var local = myFunc.findOne("+ 自选", 1000)
    if (local != null) {
        myFunc.clickCenter(local);
        text('确定').findOne(2000).click();
        sleep(1500);
        myFunc.clickCenter(local);
    } else {
        local = myWord.findWordLocation("自选");
        myWord.clickLocation(local);
        sleep(1000);
        myWord.findClickWord('确定');
        sleep(1500);
        myWord.clickLocation(local);
    }
}

function 添加一只热搜() {
    sleep(2000);
    if(!myFunc.clickByText('1', 2000)){
        click(120,560);
    }
    sleep(2000);
    自选();
    sleep(1000);
    back();
    sleep(500);
    back();
    sleep(2000);
    // var a = textContains("添加一只").findOne(1000);
    // myFunc.log("自选");
    // if (a != null && getQuWanCheng(a) != null) {
    //     click(width - 100, a.bounds().centerY())
    //     textContains("5分钟热股").findOne(1000);
    //     clickByText("NO.1");
    //     text("+ 自选").findOne(5000).click();
    //     text("已添加").findOne(5000).click();
    //     clickByText("去查看");
    //     textContains("今日任务").findOne(5000);
    // }
}


function 题材热点() {
    sleep(2000);
    var a = textStartsWith("热讯").findOnce();
    if (a != null) {
        myFunc.clickCenter(a);
    } else {
        var t = myWord.getAllword();
        for (var index = 0; index < t.length; index++) {
            var element = t[index];
            var word = element.words;
            if ((word.indexOf(":") != -1 || word.indexOf("-") != -1) && element.location.top > 300) {
                myWord.clickLocation(element.location);
                break;
            }
        }
    }
    sleep(7000);
    back();
    sleep(500);
    back();
    sleep(2000);
    // var a = textContains("题材热点").findOne(1000);
    // if (a != null && getQuWanCheng(a) != null) {
    //     click(width - 100, a.bounds().centerY())
    //     sleep(1000);
    //     textContains("题材").findOne(3000);
    //     click(743, 1699);

    //     clickByText("返回查看");
    //     textContains("今日任务").findOne(5000);
    // }
}



function 添加精选() {
    sleep(3000);
    click(200, 1700)
    sleep(2000);
    自选();
    sleep(1000);
    back();
    sleep(500);
    back();
    sleep(2000);
}

function 浏览行情() {
    var a = textContains("浏览行情").findOne(1000);
    if (a != null && getQuWanCheng(a) != null) {
        sleep(1000);
        click(width - 100, a.bounds().centerY());
        sleep(3000);
        clickByText("返回查看");
        textContains("今日任务").findOne(5000);
    }
}



function 股票扫雷() {
    sleep(7000);
    back();
    sleep(1000);
    // var a = textContains("股票扫雷").findOne(1000);
    // if (a != null && getQuWanCheng(a) != null) {
    //     click(width - 100, a.bounds().centerY())
    //     sleep(1000);
    //     textContains("扫雷").findOne(3000);
    //     click(743, 1699);
    //     sleep(2000);
    //     clickByText("返回查看");
    //     textContains("今日任务").findOne(5000);
    // }
}

function 阅读一篇资讯() {
    sleep(2000);
    // click(120, 900);
    var a = myFunc.findOnce("查看更多", 2000);
    if (a == null) {
        a = myWord.findWordLocation('查看更多');
        click(a.left, a.top + 200)
    } else {
        click(a.bounds().left, a.bounds().top + 120);
    }
    sleep(10000);
    back();
    sleep(500);
    back();
    sleep(2000);
    // myFunc.log("阅读一篇资讯");
    // var a = textContains("阅读一篇资讯").findOne(1000);
    // if (a != null && getQuWanCheng(a) != null) {
    //     click(width - 100, a.bounds().centerY())
    //     sleep(1000);
    //     click(width / 2, 1360)
    //     text("收藏").findOne(5000);
    //     sleep(8000);
    //     clickByText("返回查看");
    //     textContains("今日任务").findOne(5000);
    // }
}

function 浏览社区帖子() {
    sleep(2000);
    var a = myFunc.findOne('社区首页', 2000);
    if (a != null) {
        myFunc.clickCenter(a);
    } else {
        myWord.findClickWord("社区首页", 1, false);
    }
    sleep(10000);
    back();
    sleep(500);
    back();
    sleep(2000);
    // var a = textContains("浏览社区帖子").findOne(1000);
    // if (a != null && getQuWanCheng(a) != null) {
    //     click(width - 100, a.bounds().centerY())
    //     text("社区首页").findOne(5000).click();
    //     textContains("我的社区消息").findOne(5000);
    //     sleep(500);
    //     // ttext("关注").findOnce().parent().parent().click();
    //     text("关注").findOne(5000);
    //     clickByText("返回查看");
    //     textContains("今日任务").findOne(5000);
    // }
}

function 观看视频() {
    var b = device.getMusicVolume();
    device.setMusicVolume(0);

    sleep(4000);
    click(545, 960);
    sleep(1000);
    click(545, 960);
    sleep(15000);
    back();
    sleep(2000);
    device.setMusicVolume(b);
}



function 共享一只股票() {
    myFunc.log("共享一只股票");
    var a = textContains("共享一只股票").findOne(1000);
    if (a != null && getQuWanCheng(a) != null) {
        click(width - 100, a.bounds().centerY())
        sleep(800);
        textContains("选股").findOne(1000);
        sleep(500);
        click(217, 997);
        text("最　高").findOne(5000);
        sleep(500);
        click(1360, 200);
        clickByText("发送给朋友");
        sleep(1000);
        back()
        sleep(1000);
        myFunc.clickCenter(textContains("去查看").findOne(5000));
        textContains("今日任务").findOne(5000);
    }
}

function 分享福利中心() {
    myFunc.log("分享福利中心");
    var a = textContains("分享福利中心").findOne(1000);
    if (a != null && getQuWanCheng(a) != null) {
        click(width - 100, a.bounds().centerY())
        sleep(1000);
        click(1360, 200);
        clickByText("发送给朋友");

        sleep(1000);

        back()

        textContains("今日任务").findOne(5000);
    }
}

function 共享一篇资讯() {
    back();
    text("咨询").findOne(5000).parent().click();
    // myFunc.log("共享一篇资讯");
    // var a = textContains("共享一篇资讯").findOne(1000);
    // if (a != null && getQuWanCheng(a) != null) {
    //     click(width - 100, a.bounds().centerY())
    text("要闻").findOne(5000);
    sleep(500);
    click(width / 2, height * 2 / 3)
    text("分享").findOne(5000);
    sleep(500);
    click(1340, 200)
    clickByText("发送给朋友");

    sleep(1000);

    back()

    myFunc.clickCenter(textContains("去查看").findOne(5000));
    textContains("今日任务").findOne(5000);
    // }
}

function 共享一个社区帖子() {
    sleep(1000);
    back();
    // var a = textContains("共享一个社区帖子").findOne(1000);
    // if (a != null && getQuWanCheng(a) != null) {
    // click(width - 100, a.bounds().centerY())
    text("社区首页").findOne(5000).click();
    textContains("我的社区消息").findOne(5000);
    sleep(500);
    textStartsWith("关注").findOne(1000).parent().parent().click();
    text("评论").findOne(5000);
    sleep(500);
    click(1340, 200)
    clickByText("发送给朋友");

    sleep(1000);

    back()
    myFunc.clickCenter(textContains("去查看").findOne(5000));
    textContains("今日任务").findOne(5000);
    // }
}

function 猜涨跌() {
    sleep(1000);
    if (textContains("已猜").findOne(2000) == null) {
        text("看涨").clickable().findOne(5000).click();
    }
    back();
    sleep(500);
    back();
    sleep(2000);
}

function 点赞() {
    var a = textContains("点赞").findOne(1000);
    if (a != null && getQuWanCheng(a) != null) {
        sleep(1000);
        click(width - 100, a.bounds().centerY());
        textContains("社区首页").findOne(2000);
        sleep(1000);
        className("android.view.View").indexInParent(1).depth(28).clickable(true).findOnce().click();
        sleep(500);
        clickByText("去查看");
        textContains("今日任务").findOne(5000);
    }

}

function clickByText(str) {
    var a = textContains(str).findOne(5000);
    myFunc.log("点", a.text());
    sleep(500);
    myFunc.clickCenter(a);
}

function getQuWanCheng(a) {
    var b = a.bounds();
    var c = 80;
    var ret = text("去完成").boundsInside(1000, b.top - c, 1440, b.bottom + c).findOnce();
    myFunc.log(a.text(), "去完成==null", ret == null);
    if (ret != null) {
        ret.click();
    }
    return ret;
}














